package com.efast.cafe.portal.controller.base;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.portal.bean.base.PortalBaseLovBean;
import com.efast.cafe.portal.entity.base.PortalBaseLov;
import com.efast.cafe.portal.entity.base.PortalBaseParameter;
import com.efast.cafe.portal.remote.common.LovService;
import com.efast.cafe.portal.remote.common.ParameterService;
import com.efast.cafe.portal.service.common.ILovService;


/******************************************************************************
 * @File name   :      CommonLovController.java
 *
 * @Author      :      xl
 *
 * @Date        :      2016-8-16
 *
 * @Copyright Notice: 
 * Copyright (c) 2016 Efast Serv. All  Rights Reserved.
 * This software is published under the terms of the Efast Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2016-8-16 下午6:03:57        xl     1.0            Initial Version
 *****************************************************************************/
@Controller
@RequestMapping("/frame_center/base")
public class CommonLovController{
	@Autowired
	private ILovService lovService;

	
	@Autowired
	private ParameterService parameterService;
	

	/**
	 * 通过paramKey 查询所有以paramKey 为父节点下一级子节点
	 * @Date : 2016-7-8
	 * @param paramKey 参数key
	 * @return
	 */
	@RequestMapping(value = "/common/alllov")
	public @ResponseBody List<PortalBaseLovBean> getNextNodeLov(ModelMap model, 
			HttpServletRequest request, 
			HttpServletResponse response,@RequestParam(required = false)String paramKey){
		RequestContext context=new RequestContext(request);
		PortalBaseParameter parameter =	parameterService.api_getParameter(paramKey, CommonConstant.ModuleName.GLOBAL);
		
		
		Map<Long, PortalBaseLovBean> treeMap = new LinkedHashMap<Long, PortalBaseLovBean>();
		PortalBaseLovBean root = new PortalBaseLovBean();
		if(parameter!=null){
			List<PortalBaseLov> list= lovService.getNextNodeLov(parameter);
			for (PortalBaseLov portalBaseLov : list) {
				PortalBaseLovBean dto = new PortalBaseLovBean();
				dto.setPortalBaseLovId(portalBaseLov.getPortal_base_lov_id());
				dto.setParamKey(portalBaseLov.getParam_key());
				dto.setOptionKey(portalBaseLov.getOption_key());
				dto.setDataType(portalBaseLov.getData_type());
				dto.setId(portalBaseLov.getId());
				dto.setOptionDesc(context.getMessage("common.label.tip."+portalBaseLov.getOption_key()));
				dto.setParentId(portalBaseLov.getParent_id()!=0L?portalBaseLov.getParent_id():null);
				dto.setModuleName(portalBaseLov.getModule_name());
				dto.setI18n_param_key(portalBaseLov.getI18n_param_key());
				dto.setI18n_option_desc(portalBaseLov.getI18n_option_desc());
				treeMap.put(portalBaseLov.getPortal_base_lov_id(), dto);
			}
			Iterator<Map.Entry<Long, PortalBaseLovBean>> it = treeMap.entrySet().iterator();
			
			while (it.hasNext()) {
				Map.Entry<Long, PortalBaseLovBean> entry = it.next();
				PortalBaseLovBean tree = entry.getValue();
				if (tree.getParentId()==null) {
					root.getNodes().add(tree);
				}else{
					PortalBaseLovBean parent = treeMap.get(tree.getParentId());// 根据父Id查询
					if (parent != null) {
						parent.getNodes().add(tree);
					}
				}
			}
		}
		
		
		return root.getNodes();
	}
	
	/**
	 * 通过id 查询所有以id 为父节点 下一级的子节点
	 * @Date : 2016-7-8
	 * @param paramKey 参数key
	 * @return
	 */
	@RequestMapping(value = "/common/allbyid")
	public @ResponseBody List<PortalBaseLov> getAllById(String id){
		
		return lovService.getAllById(id);
	}
	

}
